<?php
/**
 * Created by PhpStorm.
 * User: 钊鑫
 * Date: 2019/1/23
 * Time: 9:53
 */
namespace app\api\controller;

use app\api\service\Token as TokenService;
use app\api\model\Category as CategoryModel;
use app\lib\exception\Base as BaseException;
use app\lib\exception\SuccessMessage;
use think\Controller;
use think\facade\Cache;

class Base extends Controller
{
    public function _empty()
    {
        $res =[
            "code"=>"400",
            "msg"=>"访问页面不存在",
            "errorCode"=>"10001"
        ];
        return json($res);
    }

    //判断用户权限
    protected function checkScope()
    {
        TokenService::GTuserScope();//通过token从缓存中获取用户scope，并判断是否有权限
    }

    //查询院系及专业
    public function college2Class(){
        $item = CategoryModel::where('item','college')->field('id')->find();
        $data = Cache::get('college2Class');
        if(!$data) {
            $data['college'] = CategoryModel::where(['parent_id'=>$item['id'],'publish'=>'1'])->select();
            foreach($data['college'] as $k => $v) {
                $data['class'][$v['id']] = CategoryModel::where(['parent_id'=>$v['id'],'publish'=>'1'])->select();
            }
            Cache::set('college2Class',$data,60);
        }
        return json($data);
    }

    //sussess
    public function successMessage($data){
        return json(new SuccessMessage($data));
    }

    //error
    public function errorMessage($data){
        throw new BaseException(['msg' => $data]);
    }
}